package com.woniu.zsd.merchant.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.woniu.dto.SiteListDto;
import com.woniu.form.AddSiteForm;
import com.woniu.form.SiteListForm;
import com.woniu.form.UpdateSiteForm;
import com.woniu.zsd.permission.Permission.Permission;
import com.woniu.zsd.commons.core.model.Result;
import com.woniu.zsd.merchant.service.ISiteService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2025-03-27
 */
@RestController
@RequestMapping("/site")
@Api(tags = "站点管理")
public class SiteController {

    @Autowired
    private ISiteService siteService;

    @ApiOperation(value = "站点集合分页查询")
    @GetMapping("/list")
    public Result<Page<SiteListDto>> siteList(SiteListForm siteListForm) throws JsonProcessingException {
        Page<SiteListDto> siteList = siteService.siteList(siteListForm);
        return new Result<Page<SiteListDto>>().setCode(200).setMsg("查询站点成功").setData(siteList);
    }

    @ApiOperation(value = "新增站点")
    @PostMapping("/add")
    @Permission("addSite")
    public Result addSite(@Valid @RequestBody AddSiteForm addSiteForm,@RequestHeader String token) throws Exception {
        siteService.addSite(addSiteForm);
        return Result.getResult().setCode(200).setMsg("新增站点成功");
    }

    @ApiOperation(value = "删除站点")
    @DeleteMapping("/delete")
    @Permission("deleteSite")
    public Result deleteSite(
         @ApiParam(name = "id", value = "站点id")
         @RequestParam(value = "id") Integer id,
         @RequestHeader String token
    ) {
        siteService.deleteSite(id);
        return Result.getResult().setCode(200).setMsg("删除站点成功");
    }

    @ApiOperation(value = "修改站点")
    @PutMapping("/update")
    @Permission("updateSite")
    public Result updateSite(@Valid @RequestBody UpdateSiteForm updateSiteForm) {
        siteService.updateSite(updateSiteForm);
        return Result.getResult().setCode(200).setMsg("修改站点成功");
    }

    @ApiOperation(value = "根据ID查询站点")
    @GetMapping("/find/id")
    public Result<SiteListDto> findSiteById(@ApiParam(name = "id", value = "站点id") @RequestParam(value = "id") Integer id) {
        SiteListDto site = siteService.findSiteById(id);
        return new Result<SiteListDto>().setCode(200).setMsg("查询站点成功").setData(site);
    }

    @ApiOperation(value = "根据经纬度查询附近站点")
    @GetMapping("/recent")
    public Result<List<Integer>> recentSiteId(@ApiParam(name = "longitude", value = "经度")
                                              @RequestParam(value = "longitude") Double longitude,
                                              @ApiParam(name = "latitude", value = "纬度")
                                              @RequestParam(value = "latitude") Double latitude) throws IOException {
        List<Integer> siteIds = siteService.recentSiteId(longitude, latitude);
        return new Result<List<Integer>>().setCode(200).setMsg("查询最近站点成功").setData(siteIds);
    }
}
